import { RequestAPI } from '../utils/RequestAPI'
import { defineStore } from 'pinia'
import { MicroService } from '../build/MicroService'
import { StoreUtils } from '../utils/StoreUtils'

export const useSystemSettings = defineStore(
  'useSystemSettings',
  {
    state() {
      return {}
    },
    actions: {
      async getWorkingCenterList(payload = {}) {
        try {
          let api = new RequestAPI({})
          const res = await api.get('/baseproductionarchives/tree', {
            ...payload
          })

          const { data, msg, code } = res
          return {
            msg,
            code,
            Rows: data
          }
        } catch (e) {
          throw new Error(e)
        }
      },
      async getSystemConfig(payload = {}) {
        try {
          let api = new RequestAPI({
            service: MicroService.TERMINAL
          })
          const res = await api.get('terminal/systemSetting', {
            ...payload
          })

          const { data, msg, code } = res
          return {
            msg,
            code,
            Rows: data
          }
        } catch (e) {
          throw new Error(e)
        }
      },
      async getBiiList(payload = {}) {
        try {
          let api = new RequestAPI({})
          const res = await api.get('bllclass', {
            ...payload
          })

          const { data, msg, code } = res
          const { Rows = [], Total = 0 } = data || {}

          return {
            msg,
            code,
            Rows,
            Total
          }
        } catch (e) {
          throw new Error(e)
        }
      },
      async getBwlList(payload = {}) {
        try {
          let api = new RequestAPI({})
          const res = await api.get('basewarehousearchives', {
            ...payload
          })

          const { data, msg, code } = res
          const { Rows = [], Total = 0 } = data || {}

          return {
            msg,
            code,
            Rows,
            Total
          }
        } catch (e) {
          throw new Error(e)
        }
      },
      async getUserList(payload = {}) {
        try {
          let api = new RequestAPI({})
          const res = await api.get('users/page', {
            ...payload
          })

          const { data, msg, code } = res
          const { Rows = [], Total = 0 } = data || {}

          return {
            msg,
            code,
            Rows,
            Total
          }
        } catch (e) {
          throw new Error(e)
        }
      },
      async getDeptList(payload = {}) {
        try {
          let api = new RequestAPI({})
          const res = await api.request({
            url: StoreUtils.parseUrl("/companies/21db6a365d2f1254dac92f9ec314deaa/departments/tree"),
            method: "get",
            params: {
              ...payload
            }
          })

          const { data: Rows = [], Total = 0, msg, code } = res

          return {
            Rows,
            Total,
            msg,
            code
          }
        } catch (e) {
          throw new Error(e)
        }
      },
    }
  })

